package com.sea.finance.service.impl;


import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.Inventory;
import com.sea.finance.entity.Payment;
import com.sea.finance.mapper.InventoryMapper;
import com.sea.finance.mapper.PaymentMapper;
import com.sea.finance.service.InventoryService;
import com.sea.finance.service.PaymentService;
import com.sea.operate.entity.Related;
import com.sea.operate.entity.RelatedSku;
import com.sea.operate.service.RelatedSkuService;
import com.sea.physical.entity.CreateDespatchSku;
import com.sea.physical.entity.CreateSku;
import com.sea.physical.service.CreateDespatchSkuService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.supply.entity.ProducePayment;
import com.sea.supply.service.ProducePaymentService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentMapper paymentMapper;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private ProducePaymentService producePaymentService;

    @Override
    public List<Payment> findByCnd(Payment payment) {
        return paymentMapper.findByCnd(payment);
    }

    @Override
    public int save(Payment payment) {
        return paymentMapper.insert(payment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importPayment(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<Payment> list = BatchImportUtils.iteratorForPayment(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少新增一条明细！");
            } else {
                for (Payment payment : list) {
                    Inventory inventory = new Inventory();
                    inventory.setInventoryCreditNumber(payment.getPaymentNumber());
                    inventory.setInventorySku(payment.getPaymentSku());
                    inventory.setInventoryChannelName(payment.getPaymentChannelName());
                    inventory.setInventoryDate(payment.getPaymentDate());


                    //更新本期出库数据
                    updateCredit(inventory);
                    //更新期末数据
                    updateEnd(inventory);
                    save(payment);
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importDealPayment(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<ProducePayment> list = BatchImportUtils.iteratorForDealPayment(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少新增一条明细！");
            } else {
                for (ProducePayment payment : list) {
                    ProducePayment p = producePaymentService.findByProducePayment(payment);
                    if(null==p){
                        throw new RuntimeException("暂未查询到该条信息 PO【"+p.getProduceNo()+"】 金额【"+p.getProducePayment()+"】");
                    }
                    p.setProducePaymentType(1);
                    producePaymentService.usave(p);
                }
            }
        }
    }

    //更新本期出库数据
    void updateCredit(Inventory inventory) {
        Inventory inv = inventoryService.findBySkuChannelDate(inventory);
        if (null == inv) {
            throw new RuntimeException("【SKU:" + inventory.getInventorySku() + ";日期:" + inventory.getInventoryDate() + "】不存在!");
        } else {
            boolean flag = Double.isNaN((inv.getInventoryBeginMoney() + inv.getInventoryBorrowMoney()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber()));
            if (flag){
                inventory.setInventoryCreditPrice(0.00);
            }else{
                inventory.setInventoryCreditPrice((inv.getInventoryBeginMoney() + inv.getInventoryBorrowMoney()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber()));
            }

            boolean flag2 = Double.isNaN(inventory.getInventoryCreditNumber() * inventory.getInventoryCreditPrice());
            if(flag2){
                inventory.setInventoryCreditMoney(0.00);
            }else{
                inventory.setInventoryCreditMoney(inventory.getInventoryCreditNumber() * inventory.getInventoryCreditPrice());
            }

        }

        Integer result = inventoryService.updateCreditNumberBySkuChannelDate(inventory);
        if (null == result || result == 0) {
            throw new RuntimeException("【SKU:" + inventory.getInventorySku() + ";日期:" + inventory.getInventoryDate() + "】不存在!");
        }
    }

    //更新期末数据
    void updateEnd(Inventory inventory) {
        Integer result=0;
        Inventory inv = inventoryService.findBySkuChannelDate(inventory);
        inventory.setInventoryEndNumber(inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() - inv.getInventoryCreditNumber());
        inventory.setInventoryEndPrice(inventory.getInventoryCreditPrice());
        inventory.setInventoryEndMoney(inv.getInventoryBeginMoney() + inv.getInventoryBorrowMoney() - inventory.getInventoryCreditMoney());
        try{
            inventory.setInventoryEndUnitPrice((inv.getInventoryBeginUnitPrice() * inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() * inv.getErpProduceUnitPrice()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber()));
            inventory.setInventoryEndCustomTax((inv.getInventoryBeginCustomTax() * inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() * inv.getCreateCustomTax()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber()));
            inventory.setInventoryEndCustomFreight((inv.getInventoryBeginCustomFreight() * inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() * inv.getCreateCustomFreight()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber()));
            result = inventoryService.updateEndBySkuChannelDate(inventory);
        }catch (Exception e) {
            inventory.setInventoryEndUnitPrice(0.00);
            inventory.setInventoryEndCustomTax(0.00);
            inventory.setInventoryEndCustomFreight(0.00);
            result = inventoryService.updateEndBySkuChannelDate(inventory);
        }



        if (null == result || result == 0) {
            throw new RuntimeException("【SKU:" + inventory.getInventorySku() + ";日期:" + inventory.getInventoryDate() + "】不存在!");
        }
    }
}
